package com.gaoshi.chapter5;

import java.time.LocalDate;
import java.util.Objects;

public class Employee {
    private String name;
    private double salary;
    private LocalDate hireDay;

    public Employee() { }

    public Employee(String name, double salary, int year, int month, int day) {
        this.name = name;
        this.salary = salary;
        this.hireDay = LocalDate.of(year, month, day);
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    /**
     * final修饰的方法，子类不能覆盖
     */
    public final String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public LocalDate getHireDay() {
        return hireDay;
    }

    public double raiseSalary(double byPercent) {
        this.salary += salary * (1 + byPercent) / 100;
        return this.salary;
    }

    @Override
    public String toString() {
        return getClass().getName() +
                "{name='" + name + '\'' +
                ", salary=" + salary +
                ", hireDay=" + hireDay +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        // 不用this.name.equals(employee.name)和this.hireDay.equals(employee.hireDay)是为了防备这俩属性可能为 null 的情况
        return Objects.equals(this.name, employee.name) && this.salary == employee.getSalary()
                && Objects.equals(this.hireDay, employee.hireDay);
    }

    @Override
    public int hashCode() {
        // 默认
//        return 7 * name.hashCode() + 11 * new Double(salary).hashCode() + 13 * hireDay.hashCode();
        // 使用 null 安全的 Objects.hashCode，使用静态的 Double.hashCode 来避免创建 Double 对象
//        return 7 * Objects.hashCode(name) + 11 * Double.hashCode(salary) + 13 * Objects.hashCode(hireDay);
        // 更好的方法，由提供的所有对象的散列码组合返回
        return Objects.hash(name, salary, hireDay);
    }
}
